<template>
  <div :class="$style.wrap">
    <Button type="primary" @click="showHandle">申诉</Button>
    <el-dialog :visible.sync="visible" title="申诉" :close-on-click-modal="false" @close="resetForm">
      <el-form ref="form" :model="formData" :rules="formRules" label-width="80px">
        <el-form-item prop="appealCause" label="申诉原因">
          <el-input v-model="formData.appealCause" type="textarea" :autosize="{minRows:3}"></el-input>
        </el-form-item>
        <el-form-item>
          <Button type="primary" @click="confirmHandle">确定</Button>
          <Button type="primary" @click="visible=false">取消</Button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { qualityCheckTaskAppeal } from '@/api/inspection'
export default {
  name: 'Allege',
  props: {
    selectionList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      visible: false,
      formData: {
        appealCause: ''
      },
      formRules: {
        appealCause: [{ required: true, message: '请输入申诉原因', trigger: 'change' }]
      }
    }
  },
  methods: {
    confirmHandle() {
      return this.$refs.form.validate().then(() => {
        const data = {
          appealCause: this.formData.appealCause,
          id: this.selectionList[0].id
        }
        return qualityCheckTaskAppeal(data).then(res => {
          this.visible = false
          this.$emit('on-refresh')
        })
      })
    },
    showHandle() {
      return this.$checkSelected(this.selectionList).then(() => {
        const d = this.selectionList[0]
        if ([1, '1'].includes(d.isAppeal)) {
          this.$message.warning('任务已申诉过！')
          return
        }
        if (!([3, '3'].includes(d.state))) {
          this.$message.warning('任务未质检！')
          return
        }
        this.visible = true
      })
    },
    resetForm() {
      this.$refs.form.resetFields()
    }
  }
}
</script>
<style lang="scss" module>
.wrap{
  display: inline-block;
}
</style>
